<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class refund_mod extends module
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        $this->spid = @$_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function get_list($arr)
    {
        $page = isset($arr['page']) ? $arr['page'] : 1;
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : 20;
        $page_type = isset($arr['page_type']) ? $arr['page_type'] : 1;
        $oid = isset($arr['oid']) ? true : false;
        $is_shop = !empty($arr['is_shop']) ? true : false;
        if ($is_shop) {
            $wsql = ' where shop_id = ' . $this->spid;
        } else {
            $wsql = " where uid = " . $this->uid;
        }
        $all_list = $this->db->get_all("select * from " . table('refund') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($all_list) {
            foreach ($all_list as $k => $v) {
                $ser_id[] = "'" . $v['service_id'] . "'";
            }
            if (isset($ser_id)) {
                $ser_in = implode(',', $ser_id);
                $relist = $this->db->get_all("select * from " . table('order_goods') . " where service_id in (" . $ser_in . ")");
                if ($relist) {
                    foreach ($relist as $v) {
                        $rarr[$v['service_id']] = $v;
                    }
                }
            }
            foreach ($all_list as $key => $val) {
                $val['title'] = $rarr[$val['service_id']]['title'];
                $val['images'] = $rarr[$val['service_id']]['images'];
                $val['addtime'] = _date($val['addtime']);
                $val['status_cn'] = $this->refund_status($val['status']);
                $result[] = $val;
            }
            $data['list'] = $result;
            $total = $this->db->get_one("select count(id) as total from " . table('refund') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total'], 'page_type' => $page_type));
            }
            return $data;
        }
    }
    function get_one($arr)
    {
        $service_id = isset($arr['service_id']) ? $arr['service_id'] : false;
        $is_shop = isset($arr['is_shop']) ? true : false;
        if ($is_shop) {
            $wsql = 'shop_id = ' . $this->spid;
        } else {
            $wsql = 'uid = ' . $this->uid;
        }
        $row = $this->db->get_one("select * from " . table('refund') . " where service_id = '" . $service_id . "' and " . $wsql . " limit 1");
        if ($row['id']) {
            $row['error'] = '0';
            $row['reason_cn'] = $this->refund_reason($row['reason']);
            $row['process'] = $this->refund_process($row['id']);
            $row['addtime'] = _date($row['addtime']);
            $row['passtime'] = _date($row['passtime']);
            $row['endtime'] = _date($row['endtime']);
            return $row;
        } else {
            $ogrow = $this->db->get_one("select id,oid,service_id,order_status from " . table('order_goods') . " where service_id = '" . $service_id . "'");
            if ($ogrow['order_status'] == '2') {
                return array('error' => '0');
            } elseif ($ogrow['order_status'] >= '4') {
                $orow = $this->db->get_one("select id,receipt_time from " . table('order_info') . " where id = " . $ogrow['oid']);
                $refund_apply_time = cfg('refund_apply_time') ? cfg('refund_apply_time') : 3600 * 24 * 7;
                if (time() - $orow['receipt_time'] < $refund_apply_time) {
                    return array('error' => '0');
                } else {
                    return array('error' => '1', 'msg' => '维权订单已经超时');
                }
            } else {
                return array('error' => '1', 'msg' => '当前订单状态无售后');
            }
        }
    }
    public function refund_goods($arr)
    {
        $service_id = isset($arr['service_id']) ? $arr['service_id'] : '';
        $is_shop = isset($arr['is_shop']) ? $arr['is_shop'] : '0';
        if ($is_shop) {
            $wsql = ' shop_id = ' . $this->spid . ' and ';
        } else {
            $wsql = ' uid = ' . $this->uid . ' and ';
        }
        if (!empty($wsql)) {
            $wsql = " where service_id = '" . $service_id . "' and " . rtrim($wsql, ' and ');
        }
        $grow = $this->db->get_one("select * from " . table('order_goods') . $wsql);
        if ($grow['id']) {
            $orow = $this->db->get_one("select * from " . table('order_info') . " where id = " . $grow['oid']);
            if ($orow['id']) {
                $grow['order_no'] = $orow['order_no'];
                $grow['order_status'] = $orow['order_status'];
                $grow['pay_time'] = _date($orow['pay_time']);
                $grow['goods_price'] = $grow['price'] * $grow['buy_num'];
                $grow['total_price'] = $grow['total_price'];
                $shop_row = $this->db->get_one("select id,name from " . table('shop') . " where id = " . $orow['shop_id']);
                $grow['shop_name'] = $shop_row['name'];
                $refund_price = $this->refund_price_settle($grow['price'], $orow['goods_price'], $orow['promote_price']);
                $grow['refund_price'] = _price($refund_price);
                return $grow;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    function refund_user_process($service_id)
    {
        $row = $this->db->get_one("select * from " . table('refund') . " where service_id = '" . $service_id . "' and uid = " . $this->uid);
        if ($row['id']) {
            return $this->refund_process($row['id']);
        } else {
            return false;
        }
    }
    public function refund_process($id)
    {
        $all = $this->db->get_all("select * from " . table('refund_process') . " where rid = '" . $id . "' order by addtime desc");
        if ($all) {
            foreach ($all as $v) {
                if ($v['images']) {
                    $v['images'] = unserialize($v['images']);
                }
                $v['addtime'] = _date($v['addtime']);
                $data[] = $v;
            }
            return $data;
        }
    }
    function refund_price_settle($goods_price, $goods_total_price, $promote_price = '0')
    {
        $price = $goods_price - $promote_price * $goods_price / $goods_total_price;
        return $price;
    }
    public function add_process($arr)
    {
        $sqlArr['rid'] = $arr['rid'] ? $arr['rid'] : '0';
        $sqlArr['title'] = $arr['title'] ? $arr['title'] : '';
        $sqlArr['content'] = $arr['content'] ? $arr['content'] : '';
        $sqlArr['images'] = isset($arr['images']) ? $arr['images'] : '';
        $sqlArr['addtime'] = time();
        $query = $this->db->add(table('refund_process'), $sqlArr);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }
    public function refund_reason($str = '')
    {
        $arr = array('1' => '未收到货', '2' => '已收到货', '3' => '七天无理由退换货');
        if (!empty($str) || $str === '0') {
            if (isset($arr[$str])) {
                return $arr[$str];
            } else {
                return '直接退货';
            }
        } else {
            return $arr;
        }
    }
    public function refund_status($str)
    {
        if ($str == '1') {
            return '申请退款';
        } elseif ($str == '2') {
            return '退货操作';
        } elseif ($str == '3') {
            return '卖家确认';
        } elseif ($str == '4') {
            return '平台介入';
        } elseif ($str == '5') {
            return '退款成功';
        } elseif ($str == '6') {
            return '退款失败';
        } else {
            return '取消申请';
        }
    }
}